<template>
  <div class="app-container">
    <el-row>
      <el-col :span="19">
        <el-button icon="el-icon-plus" size="medium" type="primary" @click="handleAdd()" v-permission="'admin:user:add'">新增</el-button>

        <el-button icon="el-icon-delete" size="medium" type="primary" @click="handleDeletes">删除</el-button>
      </el-col>

      <el-col :span="5">
        <el-row style="display: flex">
          <el-input v-model="pageParam.keyword" placeholder="请输入内容" style="margin-right: 20px"></el-input>
          <el-button icon="el-icon-search" size="medium" type="primary" @click="fetchData">查询</el-button>
        </el-row>
      </el-col>
    </el-row>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      element-loading-text="Loading"
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column label="账号" width="110">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户名" width="110">
        <template slot-scope="scope">
          <span>{{ scope.row.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="手机号" width="110">
        <template slot-scope="scope">
          {{ scope.row.mobile }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="邮箱" width="180">
        <template slot-scope="scope">
          {{ scope.row.email }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="性别" width="110">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gender === 1" type="success">女</el-tag>
          <el-tag v-else type="info">男</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="status-col" label="状态" width="110">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="handleStatusChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <div class="tag-group">
            <el-tag size="small" style="cursor: pointer" type="" @click="handleEdit(scope.row.id)">编辑</el-tag>&nbsp;&nbsp;
            <el-tag size="small" style="cursor: pointer" type="danger" @click="handleDelete(scope.row)">删除</el-tag>&nbsp;&nbsp;
            <el-tag size="small" style="cursor: pointer" type="success">分配角色</el-tag>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :limit.sync="pageParam.pageSize" :page.sync="pageParam.pageNo" :total="total"
                @pagination="fetchData"/>

    <el-dialog :title="userFormTitle" :visible.sync="userFormShow" width="500px">
      <el-form :model="userForm" label-width="80px">
        <el-form-item label="账号">
          <el-input v-model="userForm.username" wu></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="userForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <template>
            <el-radio v-model="userForm.gender" :label="0">男</el-radio>
            <el-radio v-model="userForm.gender" :label="1">女</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="userForm.mobile"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="userForm.status"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="userForm.email"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-checkbox-group v-model="userForm.roleIds">
            <el-checkbox v-for="role in roleSelect" :key="role.id" :label="role.id">{{ role.name }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="userFormShow = false">取 消</el-button>
        <el-button type="primary" @click="saveUserInfo()">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import {createUserInfo, deletes, listPage, updateStatus, updateUserInfo, userDetail} from '@/api/admin/user'
import {roleSelect} from '@/api/admin/role'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  components: {Pagination},
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      roleSelect: [],
      checkedIds: [],
      userFormTitle: '',
      userFormShow: false,
      userForm: {
        id: '',
        username: '',
        nickname: '',
        gender: 0,
        mobile: '',
        status: 1,
        email: '',
        roleIds: []
      },
      list: null,
      total: 0,
      listLoading: true,
      pageParam: {
        pageNo: 1,
        pageSize: 10,
        keyword: ''
      }
    }
  },
  created() {
    this.fetchData()
    this.loadRoleIds()
  },
  methods: {
    handleDeletes() {
      if (this.checkedIds.length === 0) {
        this.$message({
          type: 'warning',
          message: '请勾选需要删除的记录!'
        })
        return
      }
      this.$confirm(`确定执行删除操作吗？`, '是否继续?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '算了吧',
        type: 'warning'
      }).then(() => {
        deletes(this.checkedIds).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    },
    handleSelectionChange(values) {
      this.checkedIds = []
      values.map(res => this.checkedIds.push(res.id))
      console.log(this.checkedIds, '=======')
    },
    saveUserInfo() {
      if (this.userForm.id) {
        updateUserInfo(this.userForm.id, this.userForm).then(res => {
          this.$message({
            type: 'success',
            message: '更新成功!'
          })
          this.userFormShow = false
          this.fetchData()
        })
      } else {
        createUserInfo(this.userForm).then(res => {
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          this.userFormShow = false
          this.fetchData()
        })
      }
    },
    loadRoleIds() {
      roleSelect().then(res => {
        this.roleSelect = res.data
      })
    },
    handleDelete(row) {
      this.$confirm(`确定删除<${row.username}>用户吗？`, '是否继续?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '算了吧',
        type: 'warning'
      }).then(() => {
        deletes([row.id]).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    },
    handleEdit(id) {
      this.$common.clear(this.userForm)
      this.userFormTitle = '编辑用户信息'
      userDetail(id).then(res => {
        this.userForm = res.data
        this.userFormShow = true
      })
    },
    handleAdd() {
      this.$common.clear(this.userForm)
      this.userForm.status = 1
      this.userFormTitle = '添加用户信息'
      this.userFormShow = true
    },
    handleStatusChange(row) {
      const statusText = row.status === 1 ? '启用' : '禁用'
      this.$confirm(`此操作将${statusText}该用户`, '是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateStatus(row.id, row.status).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
        row.status = row.status === 1 ? 0 : 1
      })
      console.log(row.status, '======')
    },
    fetchData() {
      this.listLoading = true
      listPage(this.pageParam).then(response => {
        console.log(response.data, '==========')
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}
</style>
